Kenneth Tay
Oct 4, 2018
TRUE
or FALSE
)c()
function, or using the :
shortcutvec <- c(10, 5, 20)
vec <- 1:10 * 2
vec
## [1] 2 4 6 8 10 12 14 16 18 20
vec[c(1,5)]
## [1] 2 10
list()
functioncars <- list(make = "Honda",
models = c("Fit", "CR-V", "Odyssey"),
available = c(TRUE, TRUE, TRUE))
[[
or $
notation to refer to a specific key-value paircars$make
## [1] "Honda"
cars[["models"]]
## [1] "Fit" "CR-V" "Odyssey"
df <- data.frame(votes_dem = c(486351, 318, 5904),
votes_gop = c(91189, 211, 10239))
df
## votes_dem votes_gop
## 1 486351 91189
## 2 318 211
## 3 5904 10239
is.list(df)
## [1] TRUE
df$votes_dem
## [1] 486351 318 5904
3 different types of syntax:
+
syntax for plotting with ggplot2
(Session 3)%>%
syntax for transforming data with dplyr
(Session 4)A function is a named block of code which
We’ve already seen a number of functions in R! For example,
is.character("123")
## [1] TRUE
The function is.character
takes the input given to it in the parentheses and returns TRUE
or FALSE
, depending on whether the input is of type character or not.
Others we’ve seen: str
, log
, typeof
, rm
, c
, list
, length
, …
We can see what a function does by typing in ?
followed by the function name in the R console.
?is.character
A function call consists of:
mean()
: An exampleTake the mean of c(1,3,NA)
.
mean(c(1,3,NA))
## [1] NA
mean(c(1,3,NA), na.rm = TRUE)
## [1] 2
sample()
: Descriptionsample()
: UsageWhat comes after the =
sign: default value for that argument
sample()
: Argumentssample()
: Detailssample()
: Valuesample(x = 1:10, size = 10)
## [1] 4 9 7 6 10 1 8 5 3 2
sample(1:10, 10, TRUE)
## [1] 9 2 2 2 10 1 2 4 8 3
sample(size = 5, 1:10)
## [1] 5 1 3 4 2
Commands are evaluated “from inside out”
is.character(as.character(123))
## [1] TRUE
base
, datasets
, graphics
, stats
fueleconomy
: Package information on CRANhttps://cran.r-project.org/web/packages/fueleconomy/index.html
Optional material
Example: What is the line of code below trying to do?
x <- c(4, 234, 1, 50, 764)
x <- (x - min(x)) / (max(x) - min(x))
#> [1] 0.003931848 0.305373526 0.000000000 0.064220183 1.000000000
rescale01 <- function(x) {
(x - min(x)) / (max(x) - min(x))
}
rescale01(c(4, 234, 1, 50, 764))
## [1] 0.003931848 0.305373526 0.000000000 0.064220183 1.000000000
list$a <- (list$a - min(list$a)) / (max(list$a) - min(list$a))
list$b <- (list$b - min(list$b)) / (max(list$b) - min(list$b))
list$b <- (list$c - min(list$c)) / (max(list$c) - min(list$c))
vs.
list$a <- rescale01(list$a)
list$b <- rescale01(list$b)
list$c <- rescale01(list$c)
Can you spot the mistake in the first block?
list$a <- (list$a - min(list$a)) / (max(list$a) - min(list$a))
list$b <- (list$b - min(list$b)) / (max(list$b) - min(list$b))
list$c <- (list$c - min(list$c)) / (max(list$c) - min(list$c))
vs.
rescale01 <- function(x) {
(x - min(x)) / (max(x) - min(x))
}
list$a <- rescale01(list$a)
list$b <- rescale01(list$b)
list$c <- rescale01(list$c)
What if I want to rescale the entries to be between 0 and 2 instead?
dplyr
: Transform dataggplot2
: Make nice plotsreadr
: Import data into Rtidyr
: Clean datastringr
: Tools for working with character strings and regular expressionslubridate
: Make working with dates and times easiercaret
: Tools for training regression and classification modelsglmnet
: Advanced regression methodsmaps
, ggmap
: Tools for plotting spatial datashiny
: Make interactive web apps